var appPath = getAppPath();	
$(function(){
	/**
	 * 页面加载时创建grid
	 */
	$("#sensitivityGrid").datagrid({
		
		url:appPath+'/system/sensitivity_pageList.do',
		height:'full',
		striped: true,
		remoteSort:false,
		idField:'sensitivityId',
		checkbox:true,
		frozenColumns:[[
		      {field:'ck',checkbox:true},
		      {field:'sensitivityId',title:'操作',width:30,align:'center',
		    	  formatter:function(value,rowData,rowIndex){
		    		  return "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' title='编辑' onClick='showEdit("+rowIndex+")'/></a>"
		    	  }
		      }
		                ]],
		columns:[[
		      {field:'search',title:'敏感词',width:200,align:'left',sortable:true},
		      {field:'replacement',title:'替换字符',width:220,align:'left',sortable:true}
		      ]],
		 pagination:true,
		 rownumbers:true,
		 toolbar:[{
				id:'btnadd',
				text:'增加',
				iconCls:'icon-add',
				handler:function(){
					showAdd();
				}
			},'-',{
				id:'btndelMu',
				text:'删除',
				iconCls:'icon-cancel',
				handler:function(){
					var selections = $("#sensitivityGrid").datagrid('getSelections');
					if(selections == null || selections == ''){
						$.messager.alert('提示信息','请选择要删除的数据','info');
						return ;
					}else{
						$.messager.confirm('提示信息','您确定要删除这些记录？',function(r){
							if(r){
								var ids = '';
								for(var i=0;i<selections.length;i++){
									ids += selections[i].sensitivityId+',';
								}
								ids = ids.substring(0,ids.lastIndexOf(','));
								$.ajax({
									url:appPath+'/system/sensitivity_delSensitivity.do',
									data:{ids:ids},
									success:function(data){
										if(data!='ok'){
											$.messager.alert('提示信息','抱歉，删除未能完成，'+data,'error');
										}else{
											doReload("delete");
										}
									},
									error:function(XMLHttpRequest, textStatus, errorThrown){
										$.messager.alert('提示信息','抱歉，删除未能完成，'+textStatus,'error');
									}
								});		
							}
						});
					}
				}
			}]
	});
});

/**
 * 展现编辑界面
 * @param {} rowData
 */
function showEdit(rowIndex){
	var rowData = $("#sensitivityGrid").datagrid("getRows")[rowIndex];
	$('#addForm').form('clear');
	$('#addForm').form('load',rowData);
	$("#submitType").val("update");	
	$("#sensitivityId").val(rowData.sensitivityId);	
	
	$('#addDlg').window({
			title:'编辑敏感词过滤',
			iconCls:'icon-edit',
			modal: true,
			shadow: true,
			collapsible:false,
			minimizable:false,
			maximizable:false
		});
	$('#addDlg').window('move',{top:20});
	$('#addDlg').window('open');
}

/**
 * 提交数据，包括增加，修改
 */
function doPostData(){
	if(!$('#addForm').form('validate')){
		return ;
	}	
	var type = $("#submitType").val();
	var typeStr = "";
	if(type == "save"){
		typeStr = "保存";
	}else if(type == "update"){
		typeStr = "更新";
	}
	
	var subData = $('#addForm');  
    var userinfo = subData.serialize();    
	
    $.ajax({
		url:appPath+'/system/sensitivity_'+type + '.do',
		type:"post",
		data:userinfo,
		success:function(data){
			if(data!='ok'){
				if(data == 'repeat'){
					$.messager.alert('提示信息','敏感词已存在！','info');
				}else{
					$.messager.alert('提示信息','抱歉，'+typeStr+'未能完成，'+data,'error');	
				}
			}else{
				doReload(type);
				$('#addDlg').window('close');
			}
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			$.messager.alert('提示信息','抱歉，'+typeStr+'未能完成，'+textStatus,'error');
		}
	});
	
}

/**
 * 结束编辑（关闭对话框）
 */
function cancelPostData(){
	$('#addDlg').window('close');
}

function showAdd(){
	$('#addForm').form('clear');
	$("#submitType").val("save");
	$('#addDlg').window({
				title:'增加敏感词过滤',
				iconCls:'icon-add',
				modal: true,
				shadow: true,
				collapsible:false,
				minimizable:false,
				maximizable:false
			});
	$('#addDlg').window('move',{top:20});
	$('#addDlg').window('open');		
}





/**
 * grid数据重载
 */
function doReload(type){
	var ops = $('#sensitivityGrid').datagrid('getPager').pagination("options");
	if(type == "save"){
		var pn = Math.floor(ops.total/ops.pageSize)+1;
		$('#sensitivityGrid').datagrid('getPager').pagination({pageNumber:pn});
		$('#sensitivityGrid').datagrid('options').pageNumber = pn;
	}else if(type == "delete"){
		if($('#sensitivityGrid').datagrid('getRows').length == $('#sensitivityGrid').datagrid('getSelections').length){
			var pn ;
			var pnt = Math.ceil(ops.total/ops.pageSize);
			var pm = ops.pageNumber;
			if(pnt == pm){
				pn = pnt-1 ;
			}else{
				pn = pm
			}
			$('#sensitivityGrid').datagrid('getPager').pagination({pageNumber:(pn==0?1:pn)});
			$('#sensitivityGrid').datagrid('options').pageNumber = (pn==0?1:pn);
		}
	}
	$("#sensitivityGrid").datagrid('unselectAll');
	$("#sensitivityGrid").datagrid('reload');
}